package logic

import (
	"context"
	"fmt"
	"zc1/model/mysql"
	redis "zc1/model/reids"

	"zc1/zcrpc/internal/svc"
	"zc1/zcrpc/zcrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type ZcLoginLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewZcLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ZcLoginLogic {
	return &ZcLoginLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *ZcLoginLogic) ZcLogin(in *zcrpc.ZcLoginRequest) (*zcrpc.ZcLoginResponse, error) {
	// todo: add your logic here and delete this line
	get := redis.Client.Get("Smslogin" + in.Phone)
	if get.Val() != in.Sms {
		return nil, fmt.Errorf("短信验证码错误请重新输入")
	}
	var finduser mysql.User
	mysql.DB.Debug().Where("phone =?", in.Phone).Find(&finduser)
	if finduser.ID == 0 {
		return nil, fmt.Errorf("当前账号为注册请重新输入")
	}
	if finduser.Password != in.Password {
		return nil, fmt.Errorf("当前密码错误请重新输入")
	}
	redis.Client.Del("Smslogin" + in.Phone)
	return &zcrpc.ZcLoginResponse{Id: int64(finduser.ID)}, nil
}
